package com.zuo.mall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zuo.mall.common.api.CommonPage;
import com.zuo.mall.dto.PmsBrandParam;
import com.zuo.mall.entity.PmsBrand;
import com.zuo.mall.mapper.PmsBrandMapper;
import com.zuo.mall.service.PmsBrandService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand> implements PmsBrandService {
    @Override
    @Transactional
    public CommonPage<PmsBrand> listBrand(String keyword, Integer showStatus, Integer pageNum, Integer pageSize) {

        LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        if (!StrUtil.isEmpty(keyword)) {
            queryWrapper.like(PmsBrand::getName, keyword);
        }

        if (showStatus != null) {
            queryWrapper.eq(PmsBrand::getShowStatus, showStatus);
        }
        int from = (pageNum - 1) * pageSize;
        Integer total = baseMapper.selectCount(queryWrapper);
        queryWrapper.select(PmsBrand::getId, PmsBrand::getName,
                PmsBrand::getFirstLetter, PmsBrand::getSort,
                PmsBrand::getFactoryStatus, PmsBrand::getShowStatus,
                PmsBrand::getProductCount, PmsBrand::getProductCommentCount);
        queryWrapper.last("LIMIT " + from + "," + pageSize);
        List<PmsBrand> brandList = baseMapper.selectList(queryWrapper);
        CommonPage<PmsBrand> pmsBrandCommonPage = CommonPage.restPage(brandList, Long.valueOf(total), pageSize);
        return pmsBrandCommonPage;
    }

    @Override
    public int create(PmsBrandParam brandParam) {
        PmsBrand pmsBrand = new PmsBrand();
        BeanUtils.copyProperties(brandParam, pmsBrand);
        int insert = baseMapper.insert(pmsBrand);


        return insert;
    }

    @Override
    public int updateFactoryStatusBatch(List<Long> ids, Integer factoryStatus) {
        LambdaUpdateWrapper<PmsBrand> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(PmsBrand::getFactoryStatus, factoryStatus);
        updateWrapper.in(PmsBrand::getId, ids);
        boolean update = this.update(updateWrapper);
        if (update) return 1;
        return 0;
    }

    @Override
    public int update(List<Long> ids, Integer showStatus) {
        LambdaUpdateWrapper<PmsBrand> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(PmsBrand::getShowStatus, showStatus);
        updateWrapper.in(PmsBrand::getId, ids);
        boolean update = this.update(updateWrapper);
        if (update) return ids.size();
        return 0;
    }

    @Override
    public int delete(Long brandId) {
        int i = baseMapper.deleteById(brandId);
        return i;
    }
}
